package com.zzl.java8.czbk.stream.test1;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * description: Stream API之中间操作map映射（无状态）
 * @author 朱正磊
 * @date 2022-06-20 17:26:23
 */
public class StreamApiTest3 {

    /**
     * description: map()方法可以理解为集合元素映射或者集合元素转换，得到一个新流
     * @author 朱正磊
     * @date 2022-06-20 17:44:51
     */
    @Test
    void test1() {
        // 创建collection集合
        List<PbCourse> list = new ArrayList<>();
        list.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
        list.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 44));
        list.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 55));
        list.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 66));
        list.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 77));

        Stream<String> stringStream = list.stream().map(x -> x.getCourseName());
        stringStream.forEach(System.out::println);

        // 简写
        list.stream().map(PbCourse::getCourseName).forEach(System.out::println);
    }

    @Test
    void test2() {
        // 创建collection集合
        List<PbCourse> list = new ArrayList<>();
        list.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
        list.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 44));
        list.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 55));
        list.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 66));
        list.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 77));

        Stream<Map<Long, PbCourse>> mapStream = list.stream().map(x -> {
            Map<Long, PbCourse> map = new HashMap<>();
            map.put(x.getId(), x);
            return map;
        });

        // mapStream.forEach(System.out::println);

        /*mapStream.forEach(x -> {
            x.forEach((key, value) -> System.out.println(key + " = " + value));
        });*/

        // 简写
        mapStream.forEach(x -> x.forEach((key, value) -> System.out.println(key + " = " + value)));
    }

    @Test
    void test3() {
        List<PbCourse> list1 = new ArrayList<>();
        list1.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 0));
        list1.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 0));
        list1.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 0));
        list1.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 0));
        list1.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 0));

        List<PbCourse> list2 = new ArrayList<>();
        list2.add(new PbCourse(200L, "攀博课堂-Java面向对象教程", 33));
        list2.add(new PbCourse(201L, "攀博课堂-Spring全家桶教程", 44));
        list2.add(new PbCourse(202L, "攀博课堂-SpringCloud微服务教程", 55));
        list2.add(new PbCourse(203L, "攀博课堂-在线教育分布式项目教程", 66));
        list2.add(new PbCourse(204L, "攀博课堂-电子商务项目教程", 77));

        List<List<PbCourse>> allList = new ArrayList<>();
        allList.add(list1);
        allList.add(list2);

        allList.stream().map(x -> x.stream().map(item -> {
            Integer price = item.getPrice();
            if (price == 0) {
                item.setCourseName("【免费】" + item.getCourseName());
            } else {
                item.setCourseName("【收费】" + item.getCourseName());
            }
            return item;
        })).forEach(x -> x.forEach(System.out::println));
    }

    @Test
    void test4() {
        List<PbCourse> list1 = new ArrayList<>();
        list1.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 0));
        list1.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 0));
        list1.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 0));
        list1.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 0));
        list1.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 0));

        List<PbCourse> list2 = new ArrayList<>();
        list2.add(new PbCourse(200L, "攀博课堂-Java面向对象教程", 33));
        list2.add(new PbCourse(201L, "攀博课堂-Spring全家桶教程", 44));
        list2.add(new PbCourse(202L, "攀博课堂-SpringCloud微服务教程", 55));
        list2.add(new PbCourse(203L, "攀博课堂-在线教育分布式项目教程", 66));
        list2.add(new PbCourse(204L, "攀博课堂-电子商务项目教程", 77));

        List<List<PbCourse>> allList = new ArrayList<>();
        allList.add(list1);
        allList.add(list2);

        allList.stream().map(x -> x.stream().peek(item -> {
            Integer price = item.getPrice();
            if (price == 0) {
                item.setCourseName("【免费】" + item.getCourseName());
            } else {
                item.setCourseName("【收费】" + item.getCourseName());
            }
        })).forEach(x -> x.forEach(System.out::println));
    }

    @Test
    void test5() {
        List<PbCourse> list1 = new ArrayList<>();
        list1.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 0));
        list1.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 0));
        list1.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 0));
        list1.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 0));
        list1.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 0));

        List<PbCourse> list2 = new ArrayList<>();
        list2.add(new PbCourse(200L, "攀博课堂-Java面向对象教程", 33));
        list2.add(new PbCourse(201L, "攀博课堂-Spring全家桶教程", 44));
        list2.add(new PbCourse(202L, "攀博课堂-SpringCloud微服务教程", 55));
        list2.add(new PbCourse(203L, "攀博课堂-在线教育分布式项目教程", 66));
        list2.add(new PbCourse(204L, "攀博课堂-电子商务项目教程", 77));

        Stream<PbCourse> stream1 = list1.stream().peek(item -> {
            Integer price = item.getPrice();
            if (price == 0) {
                item.setCourseName("【免费】" + item.getCourseName());
            } else {
                item.setCourseName("【收费】" + item.getCourseName());
            }
        });
        stream1.forEach(System.out::println);

        Stream<PbCourse> stream2 = list2.stream().peek(item -> {
            Integer price = item.getPrice();
            if (price == 0) {
                item.setCourseName("【免费】" + item.getCourseName());
            } else {
                item.setCourseName("【收费】" + item.getCourseName());
            }
        });
        stream2.forEach(System.out::println);
    }

}
